package com.atguigu.edu.realtime.app;

import com.atguigu.edu.realtime.common.Constant;
import com.atguigu.edu.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * className: BaseAppV1
 * description: kafka source 基类
 *      通过DataStream API 向 Kafka 获取一个 topic的 Source 的基类 , 且开启 严格一次
 * author: rakan
 * date: 2022/8/31 00:09
 * version: 1.0
 */
public abstract class BaseAppV1 {

    protected abstract void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream);

    public void init(int port, int p, String ckPathGroupIdJobName, String topic){
        //操作hdfs 设置hadoop_user_name
        System.setProperty("HADOOP_USER_NAME","atguigu");

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",port);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);

        env.enableCheckpointing(3000);
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage(Constant.HDFS_OF_EDU + ckPathGroupIdJobName);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(20 * 1000);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        //env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        DataStreamSource<String> stream = env.addSource(FlinkSourceUtil.getKafkaSource(ckPathGroupIdJobName, topic));

        //数据处理，根据业务实现
        handle(env, stream);

        try {
            env.execute(ckPathGroupIdJobName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
